/*
 * This is the base panel for all the segment configuration tabs
 */
package slhs.tempo.scriptGenerator;

import java.awt.Color;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.mlc.swing.layout.LayoutConstraintsManager;

import slhs.tempo.TableAnalyzer.IntegerField;

import com.jgoodies.forms.factories.DefaultComponentFactory;

public class SegmentConfigurationPanelHandler extends JPanel implements PanelHandler {

    private static final String SEGMENT_CONFIGURATION_PANEL_LAYOUT = "SEGMENT_CONFIGURATION_PANEL_LAYOUT.XML";
    private static final String[] ENTRY_METHODS = { "Manual Entry", "Import Excel File" };
    private static final String PLACEHOLDER_TEXT = "Browse Excel File to Load";
    private String excelFilePath;

    private JPanel segmentConfigurationPanel;
    private JPanel probesDataPanel;
    private JPanel probeTimingPanel;
    private JPanel segmentConfigurationButtonsPanel;

    private SegmentConfigurationButtonsPanelHandler segmentConfigurationButtonsPanelHandler;

    private ProbeTimingPanelHandlerFactory probeTimingPanelHandlerFactory;
    private ProbeDataPanelHandlerFactory probeDataPanelHandlerFactory;

    private JButton browseFileButton;
    private JButton startManualEntryButton;

    private JTextField browseFileTextField;
    private JTextField numberOfSegmentsTextField;
    private JTextField studyNameTextField;
    private JTextField binaryDecisionTaskTextField;

    private JLabel entryMethodLabel;
    private JLabel numberOfSegmentsLabel;
    private JLabel segmentIndexLabel;
    private JLabel studyNameLabel;
    private JLabel binaryDecisionTaskLabel;

    private JComboBox entryOptionComboBox;
    private JComboBox segmentIndexComboBox;

    private DefaultComboBoxModel segmentIndexComboBoxModel;

    private Component probeTimingFormatSeperator;
    private Component probesDataSeperator;

    private List data;

    public SegmentConfigurationPanelHandler(JPanel screensHolderPanel) {
	this.probeTimingPanelHandlerFactory = new ProbeTimingPanelHandlerFactory();
	this.probeDataPanelHandlerFactory = new ProbeDataPanelHandlerFactory();
	initComponents();
	addComponents();
    }

    /**
     * 
     */
    private void addComponents() {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(SEGMENT_CONFIGURATION_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	segmentConfigurationPanel.setLayout(panelLayout);
	// here we add the controls to the container.
	segmentConfigurationPanel.add(browseFileButton, "browseFileButton");
	segmentConfigurationPanel.add(browseFileTextField, "browseFileTextField");
	segmentConfigurationPanel.add(numberOfSegmentsTextField, "numberOfSegmentsTextField");
	segmentConfigurationPanel.add(probesDataPanel, "probesDataPanel");
	segmentConfigurationPanel.add(entryMethodLabel, "entryMethodLabel");
	segmentConfigurationPanel.add(entryOptionComboBox, "entryOptionComboBox");
	segmentConfigurationPanel.add(startManualEntryButton, "startManualEntryButton");
	segmentConfigurationPanel.add(probeTimingFormatSeperator, "probeTimingFormatSeperator");
	segmentConfigurationPanel.add(probeTimingPanel, "probeTimingPanel");
	segmentConfigurationPanel.add(probesDataSeperator, "probesDataSeperator");
	segmentConfigurationPanel.add(numberOfSegmentsLabel, "numberOfSegmentsLabel");
	segmentConfigurationPanel.add(segmentConfigurationButtonsPanel, "segmentConfigurationButtonsPanel");
	segmentConfigurationPanel.add(segmentIndexLabel, "segmentIndexLabel");
	segmentConfigurationPanel.add(segmentIndexComboBox, "segmentIndexComboBox");
	segmentConfigurationPanel.add(studyNameLabel, "studyNameLabel");
	segmentConfigurationPanel.add(studyNameTextField, "studyNameTextField");
	segmentConfigurationPanel.add(binaryDecisionTaskLabel, "binaryDecisionTaskLabel");
	segmentConfigurationPanel.add(binaryDecisionTaskTextField, "binaryDecisionTaskTextField");

    }

    private void initComponents() {
	/* using a dummy base panel for now */
	segmentConfigurationPanel = new JPanel();
	segmentConfigurationButtonsPanelHandler = new SegmentConfigurationButtonsPanelHandler(this);
	segmentConfigurationButtonsPanel = segmentConfigurationButtonsPanelHandler.getPanel();
	browseFileButton = new JButton("Browse");
	browseFileTextField = new JTextField(PLACEHOLDER_TEXT);
	numberOfSegmentsTextField = new IntegerField();
	probesDataPanel = this.probeDataPanelHandlerFactory.getPanel(UtilityConstants.EXPERIMENT_TYPE);
	entryMethodLabel = new JLabel("Entry Method");
	entryOptionComboBox = new JComboBox(ENTRY_METHODS);
	startManualEntryButton = new JButton("Start Entry");
	// Use an empty string for an unlabelled separator.
	// Change to SwingConstants.RIGHT or .CENTER to change label alignment.
	probeTimingFormatSeperator = DefaultComponentFactory.getInstance().createSeparator("Probe Timing",
		SwingConstants.CENTER);
	probeTimingPanel = this.probeTimingPanelHandlerFactory.getPanel(UtilityConstants.EXPERIMENT_TYPE);
	// Use an empty string for an unlabelled separator.
	// Change to SwingConstants.RIGHT or .CENTER to change label alignment.
	probesDataSeperator = DefaultComponentFactory.getInstance().createSeparator("Probes Data",
		SwingConstants.CENTER);
	numberOfSegmentsLabel = new JLabel("Number Of Segments");
	segmentIndexComboBoxModel = new DefaultComboBoxModel();
	segmentIndexLabel = new JLabel("Segment Number");
	segmentIndexComboBox = new JComboBox(segmentIndexComboBoxModel);
	studyNameLabel = new JLabel("Study Name");
	binaryDecisionTaskLabel = new JLabel("Binary Decision Task");
	studyNameTextField = new JTextField();
	binaryDecisionTaskTextField = new JTextField();

	// configure components
	browseFileButton.setEnabled(false);
	browseFileTextField.setEnabled(false);
	browseFileTextField.setForeground(Color.GRAY);
	// action listeners
	entryOptionComboBox.addActionListener(this);
	startManualEntryButton.addActionListener(this);
	browseFileButton.addActionListener(this);

    }

    /**
     * @return
     * 
     */
    @Override
    public JPanel getPanel() {
	return segmentConfigurationPanel;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();
	if (source.equals(entryOptionComboBox)) {
	    int selectedIndex = entryOptionComboBox.getSelectedIndex();
	    if (selectedIndex == 0) {
		enableAllComponents(true);
	    } else {
		enableAllComponents(false);

	    }
	}

	if (source.equals(startManualEntryButton)) {
	    String numberOfSegments = numberOfSegmentsTextField.getText();
	    if (numberOfSegments.equals("") || (numberOfSegments == null)) {
		JOptionPane.showMessageDialog(null, "Enter a valid number in Number Of Segments Field");
		numberOfSegmentsTextField.setBackground(Color.YELLOW);
	    } else {
		fillSegmentIndexComboBox(Integer.valueOf(numberOfSegments));

	    }
	}
	if (source.equals(browseFileButton)) {
	    browseFileTextField.setText(null);
	    excelFilePath = new FileSelector(UtilityConstants.EXCEL_FILE, UtilityConstants.EXCEL_EXTENSION)
		    .getSelectedFileAbsolutePath();
	    browseFileTextField.setText(excelFilePath);
	    browseFileTextField.setForeground(Color.BLACK);
	    segmentConfigurationButtonsPanelHandler.setExcelFilePath(excelFilePath);

	}

    }

    /**
     * @param integer
     */
    private void fillSegmentIndexComboBox(Integer numberOfSegments) {
	// segmentIndexComboBox.removeAll();
	segmentIndexComboBoxModel.removeAllElements();
	for (int segmentIndex = 1; segmentIndex <= numberOfSegments.intValue(); segmentIndex++) {
	    segmentIndexComboBoxModel.addElement(segmentIndex);
	    // segmentIndexComboBox.addItem(segmentIndex);
	}
	segmentIndexComboBox.setModel(segmentIndexComboBoxModel);

    }

    /**
     * @param isEnabled
     */
    private void enableAllComponents(boolean isEnabled) {
	Component[] components = segmentConfigurationPanel.getComponents();
	for (Component currentComponent : components) {
	    currentComponent.setEnabled(isEnabled);
	    // if it's a yet another panel, do the same for all the components on that panel
	    if (currentComponent.equals(probeTimingPanel) || currentComponent.equals(probesDataPanel)) {
		JPanel currentPanel = (JPanel) currentComponent;
		Component[] subComponents = currentPanel.getComponents();
		for (Component currentSubComponent : subComponents) {
		    currentSubComponent.setEnabled(isEnabled);
		}
	    }
	}
	// some components should do just the opposite
	browseFileButton.setEnabled(!isEnabled);
	browseFileTextField.setEnabled(!isEnabled);
	// and entry options combobox should always be enabled
	entryOptionComboBox.setEnabled(true);
    }

    /**
     * @return
     */
    public List getData(String fileToBeImported) {
	ExcelImporter importer = new ExcelSegmentsImporter();
	return importer.importFile(fileToBeImported);
    }

    public String getBrowseFileTextFieldText() {
	String browseFileTextFieldText = browseFileTextField.getText();
	if ((browseFileTextFieldText.equals("")) || browseFileTextFieldText.equals(PLACEHOLDER_TEXT)
		|| (browseFileTextFieldText == null)) {
	    browseFileTextFieldText = null;
	}
	return browseFileTextFieldText;
    }

    public boolean isImportingFromExcel() {
	boolean retVal = true;
	if (entryOptionComboBox.getSelectedObjects().equals(ENTRY_METHODS[0])) {
	    retVal = false;
	}
	return retVal;
    }

    /**
     * 
     */
    public void highlightBrowseFileTextField() {
	this.browseFileTextField.setBackground(Color.YELLOW);
    }

    public String getStudyName() {
	return studyNameTextField.getText();
    }

    public String getBinaryDecisionTask() {
	return binaryDecisionTaskTextField.getText();
    }
}
